package zz.app.yzjproject

import java.util.*

/**
 * 由作者 zzq 于 2017/9/12 16:24 创建
 */
object Utils {
    fun DayOfMonth(month:Int, isLeapYear:Boolean):Int{
        when(month){
            1,3,5,7,8,10,12->return 31
            4,6,9,11->return 30
            else->{
                if(isLeapYear) return 29
                else return 28
            }
        }

    }
    fun convertDateTimeToUTCTimeSecond(year:Int,month:Int,day:Int,hour:Int,minute:Int,second:Int):Int{
        val initYear=2000
        val initMonth=1
        val initDay=1
        val initHour=0
        val initMinute=0
        val initSecond=0
        var ret=0
        for (y in initYear until year){
            if(y.isLeapYear()) ret+=31622400 else ret+=31536000
        }
        for(y in initMonth until month){
            ret+=DayOfMonth(y,year.isLeapYear())*86400
        }
        ret+=86400*(day-initDay);
        ret+=3600*(hour-initHour);
        ret+=60*(minute-initMinute);
        ret+=second-initSecond;
        return ret
    }
    fun convertTimeToUTCTimeSecond(hour:Int,minute:Int,second:Int):Int{

        val initHour=0
        val initMinute=0
        val initSecond=0
        var ret=0
        ret+=3600*(hour-initHour);
        ret+=60*(minute-initMinute);
        ret+=second-initSecond;
        return ret
    }
    fun convertUTCTimeSecondToDateTime(time:Int): DateTimeData {
        var t=time
        var year:Int
        var month:Int
        var day:Int
        var hour:Int
        var minute:Int
        year=2000
        while (t>=0) {
            if (year.isLeapYear()){
                t-=31622400
                year++
            }else{
                t-=31536000
                year++
            }
        }
        year--
        val ly=year.isLeapYear()
        if(ly)t+=31622400
        else t+=31536000
        month=1
        while(t>=0) {
            t-=DayOfMonth(month,ly)*86400
            month++
        }
        month--
        t+= DayOfMonth(month,ly)*86400
        day=1
        while(t>=0){
            t-=86400
            day++
        }
        day--
        t+=86400
        hour=0
        while(t>=0){
            t-=3600
            hour++
        }
        hour--
        t+=3600
        minute=0
        while(t>=0){
            t-=60
            minute++
        }
        minute--
        t+=60
        return DateTimeData(year,month,day,hour,minute,t)
    }
    fun distributeMPU6050Data(value: ByteArray):MPU6050Data{
        var v1:Int=value[0].ConvertToInt()+value[1].ConvertToInt()*256
        var v2:Int=value[2].ConvertToInt()+value[3].ConvertToInt()*256
        var v3:Int=value[4].ConvertToInt()+value[5].ConvertToInt()*256
        var v4:Int=value[6].ConvertToInt()+value[7].ConvertToInt()*256
        var v5:Int=value[8].ConvertToInt()+value[9].ConvertToInt()*256
        var v6:Int=value[10].ConvertToInt()+value[11].ConvertToInt()*256
        var v7:Int=value[12].ConvertToInt()+value[13].ConvertToInt()*256
        v1=v1.toSignedShort()
        v2=v2.toSignedShort()
        v3=v3.toSignedShort()
        v4=v4.toSignedShort()
        v5=v5.toSignedShort()
        v6=v6.toSignedShort()
        v7=v7.toSignedShort()
        return MPU6050Data(v1,v2,v3,v4,v5,v6,v7)
    }
    fun distributeDegreeData(value: ByteArray):DegreeData{
        return DegreeData(convertByteArrayToFloat(value[0],value[1],value[2],value[3]),
                convertByteArrayToFloat(value[4],value[5],value[6],value[7]))
    }
    fun convertByteArrayToFloat(b1:Byte,b2:Byte,b3:Byte,b4:Byte):Float{
        val v1=b4.ConvertToInt()
        val v2=b3.ConvertToInt()
        val v3=b2.ConvertToInt()
        val v4=b1.ConvertToInt()
        val S=(v1 and 0x80)>0
        val E=((v1 and 0x7f) shl 1 )+ (if(v2 and 0x80>0) 1 else 0) - 127
        var ret:Float=0f;
        for(i in 0 until 24){
            if(i==0){
                ret+=2 power E-i
            }
            if(i in 1..7) {
                if((v2 and (1 shl (7-i)))!=0){
                    ret += 2 power E-i
                }
            }
            if(i in 8..15){
                if((v3 and (1 shl (15-i)))!=0){
                    ret += 2 power E-i
                }
            }
            if(i in 16..23){
                if((v4 and (1 shl (23-i)))!=0){
                    ret += 2 power E-i
                }
            }
        }
        return if(S)-1*ret else ret


    }
    private infix fun Int.power(other:Int):Float {
        var ff = 1f
        if (other >= 0) {
            for (i in 0 until other) {
                ff *= this

            }
        }
        else{
            for(i in 0 downTo other+1)
                ff/=this
        }
        return ff
    }
    private fun Int.toSignedShort():Int = if(this<32768) this else this-65536
    fun valueToByteArray(value:Int):ByteArray {
        val v1=value%256
        val v2=(value/256)%256
        val v3=(value/65536)%256
        val v4=(value/16777216)%256
        return byteArrayOf(v1.toByte(),v2.toByte(),v3.toByte(),v4.toByte())

    }

    val beaconServiceUUID=UUID.fromString("0000FFF0-0000-1000-8000-00805F9B34FB")
    val writeCharUUID=UUID.fromString("0000FFF1-0000-1000-8000-00805F9B34FB")
    val readCharUUID=UUID.fromString("0000FFF4-0000-1000-8000-00805F9B34FB")

}